package controller

import (
	"api/app/auth/models"
	"api/libs/register"
	"encoding/json"
	"gitee.com/dreamwood/ez"
	"gitee.com/dreamwood/ez/server"
)

func (this *ApiController) RegAction(operation *server.Operation) {
	op := this.TakeOver(operation)
	content := operation.Get("content").IsString()
	appid := operation.Get("app").IsString()
	c := new(register.Page)
	_ = json.Unmarshal([]byte(content), &c)
	var createPage func(page *register.Page, parentId int)
	crud := models.PageCrud{}

	createPage = func(page *register.Page, parentId int) {
		//通过key检测是否已经有存在的记录，如果有就跳过
		find, e := crud.FindOneBy(ez.M{"route": page.Route}, nil)
		ez.PrintError(e)

		md := new(models.Page)
		if find.Id > 0 {
			md = find
		}
		md.AppId = appid
		md.Name = page.Name
		md.Route = page.Route
		md.Type = page.Type
		md.State = page.State
		md.Sort = page.Sort
		md.ParentId = parentId
		md.Icon = page.Icon
		md.Save()
		for _, son := range page.Sons {
			createPage(son, md.Id)
		}
	}
	createPage(c, 0)
	models.PageTreeModel{}.UpdateLeftAndRight(0, 0, 0)
	op.ReturnSuccess("OK", "处理完成")

}

func (this *PageController) RegAction(operation *server.Operation) {
	op := this.TakeOver(operation)
	content := operation.Get("content").IsString()
	appid := operation.Get("app").IsString()
	c := new(register.Page)
	_ = json.Unmarshal([]byte(content), &c)
	var createPage func(page *register.Page, parentId int)
	crud := models.NewPageCrud()

	createPage = func(page *register.Page, parentId int) {
		//通过key检测是否已经有存在的记录，如果有就跳过
		find, e := crud.FindOneBy(ez.M{"route": page.Route}, nil)
		ez.PrintError(e)

		md := new(models.Page)
		if find.Id == 0 {
			md.AppId = appid
			md.Name = page.Name
			md.Icon = page.Icon
			md.Route = page.Route
			md.Type = page.Type
			md.State = page.State
			md.Sort = page.Sort
			md.ParentId = parentId
			md.Save(false)
		} else {
			md = find
			md.AppId = appid
			md.Route = page.Route
			md.Name = page.Name
			md.Icon = page.Icon
			md.Save()
		}
		for _, son := range page.Sons {
			createPage(son, md.Id)
		}
	}
	createPage(c, 0)
	models.PageTreeModel{}.UpdateLeftAndRight(0, 0, 0)
	op.ReturnSuccess("OK", "处理完成")
}

func (this *ModelController) RegAction(operation *server.Operation) {
	op := this.TakeOver(operation)
	content := operation.Get("content").IsString()
	appid := operation.Get("app").IsString()
	c := new(register.Model)
	_ = json.Unmarshal([]byte(content), &c)
	var createPage func(page *register.Model, parentId int)
	crud := models.NewModelCrud()

	createPage = func(page *register.Model, parentId int) {
		//通过key检测是否已经有存在的记录，如果有就跳过
		find, e := crud.FindOneBy(ez.M{"name": page.Name}, nil)
		ez.PrintError(e)

		md := new(models.Model)
		if find.Id == 0 {
			md.AppId = appid
			md.Name = page.Name
			md.Icon = page.Icon
			md.CnName = page.CnName
			md.Index = page.Index
			md.Entry = page.Entry
			md.Login = page.Login
			md.Sort = page.Sort
			md.ParentId = parentId
			md.Save(false)
		} else {
			md = find
			md.AppId = appid
			md.Name = page.Name
			md.Icon = page.Icon
			md.CnName = page.CnName
			md.Index = page.Index
			md.Entry = page.Entry
			md.Login = page.Login
			md.Sort = page.Sort
			md.Save()
		}
		for _, son := range page.Children {
			createPage(son, md.Id)
		}
	}
	createPage(c, 0)
	models.ModelTreeModel{}.UpdateLeftAndRight(0, 0, 0)
	op.ReturnSuccess("OK", "处理完成")
}
